package cc.mrbird.febs.activity.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.mrbird.febs.activity.entity.ActivityWhite;
import cc.mrbird.febs.activity.mapper.ActivityWhiteMapper;
import cc.mrbird.febs.activity.service.IActivityWhiteService;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;

/**
 * 活动白名单表 Service实现
 *
 * @author Lord
 * @date 2020-06-06 16:24:04
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ActivityWhiteServiceImpl extends ServiceImpl<ActivityWhiteMapper, ActivityWhite>
        implements IActivityWhiteService {

    @Override
    public IPage<ActivityWhite> findActivityWhites(QueryRequest request, ActivityWhite activityWhite) {
        LambdaQueryWrapper<ActivityWhite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(activityWhite);
        Page<ActivityWhite> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<ActivityWhite> findActivityWhites(ActivityWhite activityWhite) {
        LambdaQueryWrapper<ActivityWhite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(activityWhite);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createActivityWhite(ActivityWhite activityWhite) {
        this.save(activityWhite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateActivityWhite(ActivityWhite activityWhite) {
        this.saveOrUpdate(activityWhite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivityWhite(ActivityWhite activityWhite) {
        LambdaQueryWrapper<ActivityWhite> wrapper = new LambdaQueryWrapper<>();
        wrapper.setEntity(activityWhite);
        this.remove(wrapper);
    }
}
